{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pandapower Optimal Power Flow\n",
    "This is an introduction into the usage of the pandapower optimal power flow. It shows how to set the constraints and the cost factors into the pandapower element tables.\n",
    "\n",
    "## Example Network\n",
    "\n",
    "We use the following four bus example network for this tutorial:\n",
    "\n",
    "<img src=\"pics/example_opf.png\" width=\"50%\">\n",
    "\n",
    "We first create this network in pandapower:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:51.191070Z",
     "start_time": "2025-10-20T08:09:44.410426Z"
    }
   },
   "outputs": [],
   "source": [
    "from pandapower.create import (\n",
    "    create_empty_network,\n",
    "    create_bus,\n",
    "    create_lines,\n",
    "    create_transformer,\n",
    "    create_ext_grid,\n",
    "    create_gen,\n",
    "    create_load,\n",
    "    create_poly_cost,\n",
    "    create_pwl_cost\n",
    ")\n",
    "from pandapower.run import runopp, rundcopp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:51.424847Z",
     "start_time": "2025-10-20T08:09:51.199091Z"
    }
   },
   "outputs": [],
   "source": [
    "net = create_empty_network()\n",
    "\n",
    "#create buses\n",
    "bus1 = create_bus(net, vn_kv=220.)\n",
    "bus2 = create_bus(net, vn_kv=110.)\n",
    "bus3 = create_bus(net, vn_kv=110.)\n",
    "bus4 = create_bus(net, vn_kv=110.)\n",
    "\n",
    "#create 220/110 kV transformer\n",
    "create_transformer(net, bus1, bus2, std_type=\"100 MVA 220/110 kV\")\n",
    "\n",
    "#create 110 kV lines\n",
    "create_lines(net, [bus2, bus3, bus4], [bus3, bus4, bus2], length_km=[70, 50, 40], std_type='149-AL1/24-ST1A 110.0')\n",
    "\n",
    "#create loads\n",
    "create_load(net, bus2, p_mw=60, controllable=False)\n",
    "create_load(net, bus3, p_mw=70, controllable=False)\n",
    "create_load(net, bus4, p_mw=10, controllable=False)\n",
    "\n",
    "#create generators\n",
    "eg = create_ext_grid(net, bus1, min_p_mw=-1000, max_p_mw=1000)\n",
    "g0 = create_gen(net, bus3, p_mw=80, min_p_mw=0, max_p_mw=80,  vm_pu=1.01, controllable=True)\n",
    "g1 = create_gen(net, bus4, p_mw=100, min_p_mw=0, max_p_mw=100, vm_pu=1.01, controllable=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loss Minimization\n",
    "\n",
    "We specify the same costs for the power at the external grid and all generators to minimize the overall power feed in. This equals an overall loss minimization:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:51.455549Z",
     "start_time": "2025-10-20T08:09:51.431157Z"
    }
   },
   "outputs": [],
   "source": [
    "costeg = create_poly_cost(net, 0, 'ext_grid', cp1_eur_per_mw=10)\n",
    "costgen1 = create_poly_cost(net, 0, 'gen', cp1_eur_per_mw=10)\n",
    "costgen2 = create_poly_cost(net, 1, 'gen', cp1_eur_per_mw=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We run an OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:52.901261Z",
     "start_time": "2025-10-20T08:09:51.462892Z"
    }
   },
   "outputs": [],
   "source": [
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This function runs an Optimal Power Flow using the PYPOWER OPF. To make sure that the PYPOWER OPF converges, we decrease the power tolerance `delta` (the default value is `delta=1e-10`). The power tolerance `delta` is a measure of the extent to which exceeding of minimum and maximum power limits is tolerated. That is, in above case, the limits considered by the OPF for the generators are `min_p_mw - delta` and `max_p_mw + delta` as lower and upper bound respectively on the active power. \n",
    "\n",
    "Let's check the results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:52.933108Z",
     "start_time": "2025-10-20T08:09:52.915334Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:53.118043Z",
     "start_time": "2025-10-20T08:09:53.101449Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since all costs were specified the same, the OPF minimizes overall power generation, which is equal to a loss minimization in the network. The loads at buses 3 and 4 are supplied by generators at the same bus, the load at Bus 2 is provided by a combination of the other generators so that the power transmission leads to minimal losses."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Individual Generator Costs\n",
    "\n",
    "Let's now assign individual costs to each generator.\n",
    "\n",
    "We assign a cost of 10 ct/kW for the external grid, 15 ct/kw for the generator g0 and 12 ct/kw for generator g1:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:54.966913Z",
     "start_time": "2025-10-20T08:09:54.955683Z"
    }
   },
   "outputs": [],
   "source": [
    "net.poly_cost.at[costeg, \"cp1_eur_per_mw\"] = 10\n",
    "net.poly_cost.at[costgen1, \"cp1_eur_per_mw\"] = 15\n",
    "net.poly_cost.at[costgen2, \"cp1_eur_per_mw\"] = 12"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And now run an OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:55.836801Z",
     "start_time": "2025-10-20T08:09:55.293497Z"
    }
   },
   "outputs": [],
   "source": [
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that all active power is provided by the external grid: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:55.898545Z",
     "start_time": "2025-10-20T08:09:55.871857Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:56.177748Z",
     "start_time": "2025-10-20T08:09:56.151789Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This makes sense, because the external grid has the lowest cost of all generators and we did not define any constraints.\n",
    "\n",
    "The dispatch costs are given in net.res_cost:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:56.890180Z",
     "start_time": "2025-10-20T08:09:56.871924Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transformer Constraint\n",
    "\n",
    "Since all active power comes from the external grid and subsequently flows through the transformer, the transformer is overloaded with a loading of about 145%:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:57.435100Z",
     "start_time": "2025-10-20T08:09:57.413570Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_trafo.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now limit the transformer loading to 50%:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:57.883100Z",
     "start_time": "2025-10-20T08:09:57.877326Z"
    }
   },
   "outputs": [],
   "source": [
    "net.trafo[\"max_loading_percent\"] = 50"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(the max_loading_percent parameter can also be specified directly when creating the transformer)\n",
    "and run the OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:59.245028Z",
     "start_time": "2025-10-20T08:09:58.201153Z"
    }
   },
   "outputs": [],
   "source": [
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that the transformer complies with the maximum loading:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:59.336655Z",
     "start_time": "2025-10-20T08:09:59.314284Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_trafo.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And power generation is now split between the external grid and generator 1 (which is the second cheapest generation unit):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:09:59.583350Z",
     "start_time": "2025-10-20T08:09:59.572848Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:00.244602Z",
     "start_time": "2025-10-20T08:10:00.225077Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This comes of course with an increase in dispatch costs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:00.641497Z",
     "start_time": "2025-10-20T08:10:00.616911Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Line Loading Constraints"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wen now look at the line loadings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:00.952044Z",
     "start_time": "2025-10-20T08:10:00.927089Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and run the OPF with a 50% loading constraint:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:03.177416Z",
     "start_time": "2025-10-20T08:10:01.372417Z"
    }
   },
   "outputs": [],
   "source": [
    "net.line[\"max_loading_percent\"] = 50\n",
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the line loading constraint is complied with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:03.271828Z",
     "start_time": "2025-10-20T08:10:03.252141Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And all generators are involved in supplying the loads:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:03.580344Z",
     "start_time": "2025-10-20T08:10:03.551971Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:04.249801Z",
     "start_time": "2025-10-20T08:10:04.226323Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This of course comes with a once again rising dispatch cost:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:04.592530Z",
     "start_time": "2025-10-20T08:10:04.573224Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Voltage Constraints\n",
    "\n",
    "Finally, we have a look at the bus voltage:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:04.965459Z",
     "start_time": "2025-10-20T08:10:04.932238Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_bus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and constrain it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:07.075821Z",
     "start_time": "2025-10-20T08:10:05.472815Z"
    }
   },
   "outputs": [],
   "source": [
    "net.bus[\"min_vm_pu\"] = 1.0\n",
    "net.bus[\"max_vm_pu\"] = 1.02\n",
    "runopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that all voltages are within the voltage band:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:07.341927Z",
     "start_time": "2025-10-20T08:10:07.320736Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "net.res_bus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And all generators are once again involved in supplying the loads:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:08.103013Z",
     "start_time": "2025-10-20T08:10:08.091558Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:09.133706Z",
     "start_time": "2025-10-20T08:10:09.116616Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This of course comes once again with rising dispatch costs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:09.666412Z",
     "start_time": "2025-10-20T08:10:09.646176Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DC OPF\n",
    "\n",
    "pandapower also provides the possibility of running a DC Optimal Power Flow:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:10.416827Z",
     "start_time": "2025-10-20T08:10:10.292857Z"
    }
   },
   "outputs": [],
   "source": [
    "rundcopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since voltage magnitudes are not included in the DC power flow formulation, voltage constraints cannot be considered in the DC OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:10.679876Z",
     "start_time": "2025-10-20T08:10:10.645418Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_bus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Line and transformer loading limits are however complied with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:11.023221Z",
     "start_time": "2025-10-20T08:10:10.977206Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:12.379124Z",
     "start_time": "2025-10-20T08:10:12.324796Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_trafo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As are generator limits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:13.642532Z",
     "start_time": "2025-10-20T08:10:13.558739Z"
    }
   },
   "outputs": [],
   "source": [
    "net.gen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:15.544160Z",
     "start_time": "2025-10-20T08:10:15.498492Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The cost function is the same for the linearized OPF as for the non-linear one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:16.475366Z",
     "start_time": "2025-10-20T08:10:16.450134Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Piecewise linear cost functions\n",
    "\n",
    "The OPF also offers piecewise linear cost functions. Let us first check the actual cost function setup:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:17.084237Z",
     "start_time": "2025-10-20T08:10:17.015831Z"
    }
   },
   "outputs": [],
   "source": [
    "net.poly_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An element can either have polynomial costs or piecewise linear costs at the same time. So let us first delete the polynomial costs in order to avoid confusion and errors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:17.958242Z",
     "start_time": "2025-10-20T08:10:17.918053Z"
    }
   },
   "outputs": [],
   "source": [
    "net.poly_cost.drop(net.poly_cost.index.values, inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The results above have been produced with linear polynomial cost functions. Let's try to reproduce the results using piecewise linear cost functions. Costs have to be defined for the whole range of generators and external grids:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:18.266339Z",
     "start_time": "2025-10-20T08:10:18.229797Z"
    }
   },
   "outputs": [],
   "source": [
    "net.gen[[\"min_p_mw\", \"max_p_mw\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:19.013456Z",
     "start_time": "2025-10-20T08:10:18.878289Z"
    }
   },
   "outputs": [],
   "source": [
    "net.ext_grid[[\"min_p_mw\", \"max_p_mw\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We define the piecewise linear cost as constant over the whole range to reproduce the polyomial costs defined above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:20.748292Z",
     "start_time": "2025-10-20T08:10:20.610571Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "create_pwl_cost(net, 0, \"gen\", [[0, 80, 15]])\n",
    "create_pwl_cost(net, 1, \"gen\", [[0, 100, 12]])\n",
    "create_pwl_cost(net, 0, \"ext_grid\", [[-1000, 1000, 10]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us check the results from the previous OPF again!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:21.195088Z",
     "start_time": "2025-10-20T08:10:21.020817Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:21.863596Z",
     "start_time": "2025-10-20T08:10:21.836471Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We run the same OPF now with different cost function setup. We should get the exact same results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:22.751147Z",
     "start_time": "2025-10-20T08:10:22.529170Z"
    }
   },
   "outputs": [],
   "source": [
    "rundcopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:23.015588Z",
     "start_time": "2025-10-20T08:10:22.939430Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:23.499180Z",
     "start_time": "2025-10-20T08:10:23.443133Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now lets define real piecewise linear costs for generator 1. We define the costs as 12€/MW up to 70MW and 20€/MW from 70MW to 100MW:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:24.196378Z",
     "start_time": "2025-10-20T08:10:24.164465Z"
    }
   },
   "outputs": [],
   "source": [
    "net.pwl_cost.loc[[1], \"points\"] = [[[0, 70, 12], [70, 100, 20]]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And run another OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:24.598851Z",
     "start_time": "2025-10-20T08:10:24.365133Z"
    }
   },
   "outputs": [],
   "source": [
    "rundcopp(net, delta=1e-16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can see that generator 1 only dispatches 70MW, above which generator 0 becomes less expensive and is therefore dispatched:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:24.847711Z",
     "start_time": "2025-10-20T08:10:24.795287Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Debugging\n",
    "\n",
    "For more information on the status of the OPF solver, set verbose=True:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T08:10:29.482361Z",
     "start_time": "2025-10-20T08:10:25.358699Z"
    }
   },
   "outputs": [],
   "source": [
    "runopp(net, verbose=True, delta=1e-16)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Raw Cell Format",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
